﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Utility.ModifyRegistry;

namespace Elkomp {

	public partial class FormaEdit : Form {
		protected bool FormIsEdit = false;

		public string MainConnection { set; get; }

		public string UpdateTable { set; get; }

		public string SelectSQL { set; get; }

		public string PrimaryKey { set; get; }

		public string WherePart;

		public FormaEdit() {
			ModifyRegistry registry = new ModifyRegistry();
			MainConnection = @"Data Source=" + registry.Read( "Server" ) + ";Initial Catalog=" + registry.Read( "Database" ) + ";Integrated Security=True";
			InitializeComponent();
			PrimaryKey = "Id";
			FormIsEdit = false;
			MainTab.TabPages.Remove( tabSystem );
			//this.Text = this.Parent.Text + " - Нов Внес ";
		}

		public FormaEdit( int selectedId ) {
			InitializeComponent();
			PrimaryKey = "Id";
			FormIsEdit = true;
			MainTab.TabPages.Remove( tabSystem );
		}

		private void btnZatvori_Click( object sender, EventArgs e ) {
			base.Close();
		}

		private void btnOk_Click( object sender, EventArgs e ) {
		}

		public virtual bool validacija() {
			foreach ( Control control in groupVnes.Controls ) {
				if ( control is System.Windows.Forms.TextBox ) {
					if ( control.BackColor == Color.Tomato || control.BackColor == Color.Red ) {
						control.Focus();
						return false;
					}
				}
			}

			return true;
		}

		public virtual void save_Edit() {
			this.Close();
		}

		public virtual void save_Insert() {
			this.Close();
		}

		public virtual void Oznaci_Zadolzitelni() {
			foreach ( Control control in groupVnes.Controls ) {
				if ( control is TxtNumeric.TxtNum ) {
					TxtNumeric.TxtNum c = new TxtNumeric.TxtNum();
					c = ( TxtNumeric.TxtNum )control;
					if ( c.C_ShowRedOnLeaveIfEmpty == true ) {
						if ( c.Text.Length < 1 ) {
							c.BackColor = Color.Red;
						}
					}
				}
			}
		}

		private void btnZatvori_Click_1( object sender, EventArgs e ) {
			this.Close();
		}

		private void FormaEdit_FormClosing( object sender, FormClosingEventArgs e ) {
			if ( !validacija() ) {
				e.Cancel = true;
			}
		}
	}
}